// 网格小区详情

import jf_JSON from "@/assets/json/jf.json";
import jf_peopleJSON from "@/assets/json/jf_people.json";

import mhd_JSON from "@/assets/json/mhd.json";
import mhd_peopleJSON from "@/assets/json/mhd_people.json";

import hsjf_JSON from "@/assets/json/hsjf.json";
import hsjf_peopleJSON from "@/assets/json/hsjf_people.json";

import tyxq_JSON from "@/assets/json/tyxq.json";
import tyxq_peopleJSON from "@/assets/json/tyxq_people.json";

import ycxq_JSON from "@/assets/json/ycxq.json";
import ycxq_peopleJSON from "@/assets/json/ycxq_people.json";

import sjmy_JSON from "@/assets/json/sjmy.json";
import sjmy_peopleJSON from "@/assets/json/sjmy_people.json";

const viewObj = {
  巨丰小区: {
    view: {
      heading: 22.5,
      pitch: -54.0,
      roll: 0.12,
      x: 109.741599,
      y: 38.2240564,
      z: 1652.683488,
    },
    dthJSON: jf_JSON,
    peopleInfo: jf_peopleJSON,
    labels: [],
  },
  曼哈顿小区: {
    view: {
      y: 38.237225,
      x: 109.741195,
      z: 1601.03,
      heading: 350.4,
      pitch: -45.8,
      roll: 360,
    },
    dthJSON: mhd_JSON,
    peopleInfo: mhd_peopleJSON,
    labels: [],
  },
  和顺嘉府小区: {
    view: {
      y: 38.233191,
      x: 109.742092,
      z: 1462.86,
      heading: 350,
      pitch: -37.9,
      roll: 360,
    },
    dthJSON: hsjf_JSON,
    peopleInfo: hsjf_peopleJSON,
    labels: [],
  },
  桃园小区: {
    view: {
      y: 38.241249,
      x: 109.735969,
      z: 1738.12,
      heading: 347.4,
      pitch: -52.1,
      roll: 359.9,
    },
    dthJSON: tyxq_JSON,
    peopleInfo: tyxq_peopleJSON,
    labels: [],
  },
  元驰小区: {
    view: {
      y: 38.225294,
      x: 109.734139,
      z: 1420.15,
      heading: 349.9,
      pitch: -36.7,
      roll: 360,
    },
    dthJSON: ycxq_JSON,
    peopleInfo: ycxq_peopleJSON,
    labels: [],
  },
  圣景名苑小区: {
    view: {
      y: 38.235437,
      x: 109.730668,
      z: 1463.69,
      heading: 349.7,
      pitch: -46,
      roll: 360,
    },
    dthJSON: sjmy_JSON,
    peopleInfo: sjmy_peopleJSON,
    labels: [],
  },
};

// 视角切换
function toView(name, callback) {
  const attr = viewObj[name];
  if (!attr) return;
  if (attr.view) window.vis3d.util.setCameraView(attr.view);

  if (attr.labels.length == 0)
    loadOneDTH(name, attr.dthJSON, attr.peopleInfo, callback);
}

// 加载小区单体化数据以及人员信息数据
let drawTool = undefined;
function loadOneDTH(name, dthJSON, peopleInfo, callback) {
  if (!name) return;

  if (!drawTool) {
    drawTool = new vis3d.plot.Tool(viewer, {
      canEdit: false,
    });
  }

  const features = dthJSON.features;
  for (let i = 0; i < features.length; i++) {
    let feature = features[i];
    let properties = feature.properties;
    let dnumber = properties.attr.name;

    const peopleList = peopleInfo.filter(
      (item) => item["现居住地"]?.indexOf(dnumber + "号楼") != -1
    );

    let attr = {
      xqName: name,
      dnumber: dnumber,
      peopleInfo: peopleList,
    };
    let lnglats = feature.geometry.coordinates[0];
    let center = getCenterByPositions(lnglats); // 计算面中心点
    let label = addLabel(
      Cesium.Cartesian3.fromRadians(center.longitude, center.latitude),
      dnumber + "号楼"
    );

    viewObj[name].labels.push(label);

    // 添加楼栋面
    drawTool.createByPositions({
      type: "polygon",
      style: {
        heightReference: 1,
        color: "#FFFF00",
        colorAlpha: 0.01,
      },
      positions: lnglats,
      success: function (entObj, ent) {
        entObj.attr = attr;
        ent.attr = attr;
        ent.clickType = "clickPolygon";

        ent.mouseMove = (evt, ent) => {
          ent.polygon.material =
            Cesium.Color.fromCssColorString("#FFFF00").withAlpha(0.5);
        };

        ent.unMouseMove = (evt, ent) => {
          ent.polygon.material =
            Cesium.Color.fromCssColorString("#FFFF00").withAlpha(0.01);
        };

        ent.click = (evt, ent) => {
          if (callback) callback(ent.attr);
        };

        ent.tooltip = attr.xqName + "-" + attr.dnumber + "幢";
      },
    });
  }
}

function getCenterByPositions(lnglats) {
  if (!lnglats || lnglats.length < 3) {
    console.warn("传入数组长度不够！");
    return;
  }
  let ps = [];
  for (let i = 0; i < lnglats.length; i++) {
    let lnglat = lnglats[i];
    let point = turf.point([lnglat[0], lnglat[1]]);
    ps.push(point);
  }
  let features = turf.featureCollection(ps);
  let center = turf.center(features).geometry.coordinates;
  return Cesium.Cartographic.fromDegrees(center[0], center[1]);
}

function addLabel(p, text, color, minH, maxH) {
  return viewer.entities.add({
    position: p,
    label: {
      text: text,
      font: "14pt Source Han Sans CN", //字体样式
      fillColor: color || Cesium.Color.BLACK, //字体颜色
      backgroundColor: Cesium.Color.SNOW.withAlpha(0.5), //背景颜色
      showBackground: true, //是否显示背景颜色
      style: Cesium.LabelStyle.FILL, //label样式
      outlineWidth: 2,
      // heightReference: 1,
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
        minH || 10,
        maxH || 2000
      ),
      pixelOffset: new Cesium.Cartesian2(10, 0), //偏移
    },
  });
}

export { toView };
